clear all

** open jacobian
** ------------------------------------------------------------------
insheet using "jacobian.csv"
rename v1 mindex
rename v2 quintile
rename v3 kappa
rename v4 nu_v
rename v5 nu_y
rename v6 mu_e
rename v7 sigma_e
rename v8 b1
rename v9 b2
rename v10 vomega1
rename v11 vomega0
rename v12 rho
rename v13 omega2
gen moment = ""
replace moment = "num suppliers" if mindex == 8
replace moment = "num customers" if mindex == 7
replace moment = "s.d. sales" if mindex == 6
replace moment = "sales share" if mindex == 5
replace moment = "# exporters" if mindex == 4
replace moment = "export intensity" if mindex == 3
replace moment = "unweighted supplier wage" if mindex == 2
replace moment = "weighted supplier wage" if mindex == 1


** drop omega2 whose identification is known
drop omega2

** number moments
sort moment quintile
by moment quintile: assert _N == 1
compress

** rename variables to reshape dataset
** ---------------------------------------------------------------------
capture program drop _all
program define renameV
	while "`1'" != "" {
	rename `1' sensit`1'
	mac shift
	}
end

renameV kappa nu_v nu_y vomega1 vomega0 rho mu_e sigma_e b1 b2
reshape long sensit, i(mindex moment quintile) j(param) string

** -------------------------------------------------------------------------
** normalize sensitivity by mean of moment 
** -------------------------------------------------------------------------
sort mindex quintile

** 1) standard deviation of moment block
gen asensit = abs(sensit)
by mindex: egen naux = sd(asensit)
gen nsensit = abs(sensit)/naux
label var nsensit "normalized sensitivity"
drop naux

** ----------------------------------------------------------------------------
** generate table with summary of two most sensitive moments to each parameter
** ----------------------------------------------------------------------------

** make number of suppliers and customers the same set
replace mindex = 7 if mindex == 8  
replace moment = "# business partners" if mindex == 7

//collapse (max) nsensit_max = nsensit (mean) nsensit_mean = nsensit ///
//	(median) nsensit_median = nsensit, by(param mindex moment)
collapse (max) nsensit, by(param mindex moment)

assert nsensit < .

** By parameter: maximum moment
sort param nsensit
by param: gen maxJ = nsensit[_N]
by param: gen maxJ2 = nsensit[_N-1]
by param: gen maxJ3 = nsensit[_N-2]
gen ratio = maxJ/maxJ2
by param: gen moment1 = moment[_N]
by param: gen moment2 = moment[_N-1]
by param: gen moment3 = moment[_N-2]

by param: gen tempo = 1 if _n == 1
gen tempo2 = -ratio
sort tempo2
//list param moment1 maxJ moment2 maxJ2 moment3 maxJ3 ratio if tempo == 1
//pause "Ordering 1: Two maximum moments for each parameter"
//drop maxJ maxJ2 moment1 moment2 ratio tempo tempo2

keep if tempo == 1
gen order = .
replace order = 1 if param == "kappa"
replace order = 2 if param == "nu_v"
replace order = 3 if param == "nu_y"
replace order = 4 if param == "vomega1"
replace order = 5 if param == "vomega0"
replace order = 6 if param == "rho"
replace order = 7 if param == "mu_e"
replace order = 8 if param == "sigma_e"
replace order = 9 if param == "b2"
replace order = 10 if param == "b1"
sort order
list param moment1 maxJ moment2 maxJ2 ratio if tempo == 1, sep(10)
list param moment1 maxJ moment2 maxJ2 moment3 maxJ3 if tempo == 1, sep(10)

keep if tempo == 1
keep param moment1 maxJ moment2 maxJ2 moment3 maxJ3
list param moment1 maxJ moment2 maxJ2 moment3 maxJ3, sep(10)
order param moment1 maxJ moment2 maxJ2 moment3 maxJ3
//texsave using TableA20.tex

